program ResidueProbability
implicit none

character(len=79)ARGUMENT,symbol
integer,parameter :: first=196
integer i,j,k,ios,connum
integer,allocatable :: c1(:),c2(:)
real,allocatable ::  p(:)
real res(first)

ios=0
i=0
open(11,file="nonnative.dat",status="old")
do while(.true.)
 read(11,"(A)",iostat=ios)ARGUMENT
 if(ios/=0)exit
 i=i+1
enddo
allocate(c1(i))
allocate(c2(i))
allocate(p(i))
 connum=i
! write(*,*) connum
rewind(11)
ios=0
i=0
do while(.true.)
 read(11,"(A)",iostat=ios)ARGUMENT
 if(ios/=0)exit
 i=i+1
 read(ARGUMENT(3:5),"(I3)") c1(i)
 read(ARGUMENT(10:12),"(I3)") c2(i)
 read(ARGUMENT(16:20),"(F5.3)") p(i)
enddo
close(11)

open(22,file="rNACutoff-5.5-9.5.dat",status="replace")
do j=72,first
	if(MOD(j,3)==0)then
	symbol="S"
	elseif(MOD(j,3)==1)then
	symbol="B"
	elseif(MOD(j,3)==2)then
	symbol="P"
	endif
     k=0
  do i=1,connum
   if(j==c1(i) .or. j==c2(i))then
      k=k+1
     res(j)=res(j)+p(i)
   endif
  enddo
     if(res(j)/=0)then
    write(22,"(I4,3X,F5.3,3X,1A)") j,res(j),symbol
     endif
enddo
close(22)
end program
